package com.liu.base.service;

import com.liu.base.dao.LabelDao;
import com.liu.base.pojo.Label;
import com.liu.common.utils.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class LabelService {


    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部标签
     * @return
     */
    public List<Label> findAll(){
        return labelDao.findAll();
    }
    /**
     * 根据ID查询标签
     * @return
     */
    public Label findById(String id){
        return labelDao.findById(id).get();
    }

    public void add(Label label){

        label.setId( idWorker.nextId()+"" );//设置ID
        labelDao.save(label);
    }
    /**
     * 修改标签
     * @param label
     */
    public void update(Label label){
        labelDao.save(label);
    }
    /**
     * 删除标签
     * @param id
     */
    public void deleteById(String id){
        labelDao.deleteById(id);
    }

    /**
     * 根据条件查询
     * @param label
     * @return
     */
    public List<Label> search(Label label) {
        return  labelDao.findAll(setParam(label));

    }

    /**
     * 分页+条件查询
     * @param label
     * @param page
     * @param size
     * @return
     */
    public Page<Label> searchPage(Label label, int page, int size) {
        if(page==0){
            page=1;
        }
        if (size==0){
            size=10;
        }

        return labelDao.findAll(setParam(label),PageRequest.of(page-1,size));

    }



    private Specification<Label> setParam(Label label){

        return new Specification<Label>(){

            @Nullable
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> lists = new ArrayList<>();
                if(StringUtils.isNotEmpty(label.getLabelname())){
                    Predicate predicate = criteriaBuilder.like(root.get("labelname").as(String.class),"%"+label.getLabelname()+"%");
                    lists.add(predicate);
                }
                if(StringUtils.isNotEmpty(label.getState())){
                    Predicate predicate = criteriaBuilder.equal(root.get("state").as(String.class),label.getState());
                    lists.add(predicate);
                }
                if(StringUtils.isNotEmpty(label.getId())){
                    Predicate predicate = criteriaBuilder.equal(root.get("id").as(String.class),label.getId());
                    lists.add(predicate);
                }


                Predicate [] con = new Predicate[lists.size()];
                lists.toArray(con);

                return criteriaBuilder.and(con);
            }
        };
    }
}
